package com.open4cn.simulator.answer.xrite;

import com.open4cn.simulator.core.answer.AbstractAnswer;
import com.open4cn.simulator.core.answer.AnswerModeEnum;
import com.open4cn.simulator.core.answer.AnswerResponse;
import com.open4cn.simulator.core.ext.XriteStatus;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-13 01:53
 */
public class MeasureAnswer extends AbstractAnswer {

    public MeasureAnswer() {
        super("xrite/measure", AnswerModeEnum.SUCCESS);
    }

    public AnswerResponse answer(XriteStatus xriteStatus, String instruction) {
        if (!AnswerModeEnum.FAIL.equals(getAnswerMode())) {
            getActions().forEach(action -> {
                action.action(xriteStatus, instruction);
            });
        }
        AnswerResponse response = getAnswer(xriteStatus);
        return response;
    }

    /**
     * 获取应答结果
     *
     * @param xriteStatus
     * @return
     */
    private AnswerResponse getAnswer(XriteStatus xriteStatus) {
        switch (getAnswerMode()) {
            case SUCCESS:
                return AnswerResponse.success(success(xriteStatus));
            case FAIL:
                return AnswerResponse.fail(fail(xriteStatus));
            case RANDOM:
                return AnswerResponse.success(random(xriteStatus));
            case LOOP:
                return AnswerResponse.success(getLoop());
            case INPUT:
                Object input = pollInput();
                if (null != input) {
                    return AnswerResponse.success(input);
                }
            default:
                return AnswerResponse.success(success(xriteStatus));
        }
    }


    /**
     * 成功应答
     * 1100100000000M00
     * 0.000,    0.000,    0.000,    0.000,   78.517,   82.724
     * 83.869,   84.607,   85.645,   86.104,   86.630,   87.183
     * 87.499,   88.174,   88.269,   88.515,   89.005,   89.007
     * 89.280,   89.799,   89.797,   89.937,   90.078,   90.106
     * 90.587,   90.542,   90.579,   90.575,   90.726,   90.199
     * 90.637,   91.231,   91.269,   91.486,   91.426,   91.685
     * 91.703,   91.739,   91.765,   91.213
     * ,
     * >
     *
     * @param xriteStatus
     * @return
     */
    private Object success(XriteStatus xriteStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("1")
                .append("10")
                .append(xriteStatus.getCurrentSpecularGloss().getCode())
                .append(xriteStatus.getCurrentUV().getCode())
                .append(xriteStatus.getCurrentAperture().getCode())
                .append("000000M00")
                .append("\r\n");

        builder.append("0.000,    0.000,    0.000,    0.000,   78.517,   82.724").append("\r\n");
        builder.append("83.869,   84.607,   85.645,   86.104,   86.630,   87.183").append("\r\n");
        builder.append("87.499,   88.174,   88.269,   88.515,   89.005,   89.007").append("\r\n");
        builder.append("89.280,   89.799,   89.797,   89.937,   90.078,   90.106").append("\r\n");
        builder.append("90.587,   90.542,   90.579,   90.575,   90.726,   90.199").append("\r\n");
        builder.append("90.637,   91.231,   91.269,   91.486,   91.426,   91.685").append("\r\n");
        builder.append("91.703,   91.739,   91.765,   91.213").append("\r\n");

        builder.append(",").append("\r\n").append(">");
        return builder.toString();
    }


    /**
     * 失败应答
     *
     * @param xriteStatus
     * @return
     */
    private Object fail(XriteStatus xriteStatus) {
        StringBuilder builder = new StringBuilder();
        builder.append("9")
                .append("10")
                .append(xriteStatus.getCurrentSpecularGloss().getCode())
                .append(xriteStatus.getCurrentUV().getCode())
                .append(xriteStatus.getCurrentAperture().getCode())
                .append("000000M00")
                .append("\r\n");

        builder.append("0.000,   0.000,   0.000,   0.000,   0.000,   0.000").append("\r\n");
        builder.append("0.000,   0.000,   0.000,   0.000,   0.000,   0.000").append("\r\n");
        builder.append("0.000,   0.000,   0.000,   0.000,   0.000,   0.000").append("\r\n");
        builder.append("0.000,   0.000,   0.000,   0.000,   0.000,   0.000").append("\r\n");
        builder.append("0.000,   0.000,   0.000,   0.000,   0.000,   0.000").append("\r\n");
        builder.append("0.000,   0.000,   0.000,   0.000,   0.000,   0.000").append("\r\n");
        builder.append("0.000,   0.000,   0.000,   0.000").append("\r\n");

        builder.append(",").append("\r\n").append(">");
        return builder.toString();
    }

    /**
     * 随机反射率
     *
     * @param xriteStatus
     * @return
     */
    private Object random(XriteStatus xriteStatus) {
        return success(xriteStatus);
    }
}
